package com.example.week8_mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.week8_mybatis_plus.entity.GoodsSuppliers;
import com.example.week8_mybatis_plus.entity.Supplier;
import com.example.week8_mybatis_plus.mapper.GoodsSuppliersMapper;
import com.example.week8_mybatis_plus.mapper.SupplierMapper;
import com.example.week8_mybatis_plus.service.IGoodsSuppliersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangbo
 * @since 2022-10-25
 */
@Service
public class GoodsSuppliersServiceImpl extends ServiceImpl<GoodsSuppliersMapper, GoodsSuppliers> implements IGoodsSuppliersService {
        @Autowired
        private GoodsSuppliersMapper goodsSuppliersMapper;
        @Autowired
        private SupplierMapper supplierMapper;
        @Override
        public List<Supplier> getGoodsSuppliers(Integer goodsId) {
            QueryWrapper<GoodsSuppliers> qw = new QueryWrapper<>();
            qw.eq("goods_id", goodsId);
            List<Supplier> list = new LinkedList<>();
            List<GoodsSuppliers> suppliersId = goodsSuppliersMapper.selectList(qw);
            for (GoodsSuppliers goodsSuppliers : suppliersId) {
                list.add(supplierMapper.selectById(goodsSuppliers.getSuppliersId()));
            }
            return list;
        }

        @Override
        public boolean addGoodsSuppliers(Integer goodsId, Integer suppliersId) {
            GoodsSuppliers goodsSuppliers = new GoodsSuppliers();
            goodsSuppliers.setGoodsId(goodsId);
            goodsSuppliers.setSuppliersId(suppliersId);
            return goodsSuppliersMapper.insert(goodsSuppliers) > 0;
        }
}
